﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CSM.Models;
using CSM.Controllers;
using System.Data.Linq;
using System.Collections.Generic;

namespace CSM.Views.Management
{
    public partial class UserRightSetupSelectRight :CSMPageBase
    {
        CustomDataContext db = new CustomDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TextBox_UserId.Text =HttpUtility.UrlDecode(Request.QueryString["userid"]).ToString();
                var myUserQuery = (from a in db.TCsm_JS_User where a.UserID == TextBox_UserId.Text select a).Single();
                TextBox_UserName.Text = myUserQuery.UserName;
                if (myUserQuery.UserRightID == "" || myUserQuery.UserRightID == null)
                {
                    var QueryToGridView = from b in db.TCsm_JS_Menu where b.IsLast==true
                                          select new
                                          {
                                              b.MenuID,
                                              b.MenuName,
                                              check = false
                                          };
                    GridView_RoleList.DataSource = QueryToGridView;
                    GridView_RoleList.DataBind();
                }
                else
                {
                    string[] aa = myUserQuery.UserRightID.Split(',');

                    var QueryToGridView = from b in db.TCsm_JS_Menu where b.IsLast==true
                                          select new
                                          {
                                              b.MenuID,
                                              b.MenuName,
                                              check = aa.Contains(b.MenuID.ToString())
                                          };
                    GridView_RoleList.DataSource = QueryToGridView;
                    GridView_RoleList.DataBind();

                }






           }

        }


        private string GetUserRight(string UserId)
        {

            var myRightRow = (from a in db.TCsm_JS_User where a.UserID == UserId select a).Single();
            string myright1 = myRightRow.UserRightID;
            var myroleright = (from c in db.TCsm_JS_Role where myRightRow.RoleID.Split(',').Contains(c.RoleID.ToString()) select c.RightID).ToArray();
            string[] finalright = myright1.Split(',');

            foreach (string myrigh2 in myroleright)
            {

                finalright = Join(myrigh2.Split(','), finalright);

            }
            return String.Join(",", finalright);

        }

        private string[] Join(string[] arr_str1, string[] arr_str2)
        {

            var q = (from c in arr_str1 select c).Union(from d in arr_str2 select d);
            var q1 = from x in q.ToArray() where x != string.Empty select x;
            return q1.ToArray();
        }

        protected void Button_Cancel_Command(object sender, CommandEventArgs e)
        {
            Server.Transfer("UserRightSetup.aspx");
        }

        protected void Button_Save_Command(object sender, CommandEventArgs e)
        {
            string myRight = "";

            for (int i = 0; i < GridView_RoleList.Rows.Count; i++)
            {
                GridViewRow myrows = GridView_RoleList.Rows[i];
                if (((CheckBox)myrows.Cells[2].FindControl("Checkbox_Right")).Checked)
                {
                    myRight = myRight + "," + myrows.Cells[0].Text;
                }


            }

            var myRightRow = (from a in db.TCsm_JS_User where a.UserID == TextBox_UserId.Text select a).Single();

            if (myRight != "")
            {
                string xx = myRight.Substring(1, myRight.Length - 1);
                myRightRow.UserRightID = xx;
            }
            else
            {
                myRightRow.UserRightID = "";
            }

            myRightRow.AllRightID = GetUserRight(TextBox_UserId.Text);


            try
            {
                db.SubmitChanges();
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "aa", " <script>tb_remove(); </script>");
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('更新权限成功'); </script>");
                Server.Transfer("UserRightSetup.aspx");
                
            }
            catch
            {

                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('更新权限失败'); </script>");
            }
        }



    }
}
